package org.mzy.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mzy.basic.anno.PreAuthorize;
import org.mzy.basic.util.AjaxResult;
import org.mzy.basic.util.PageList;
import org.mzy.org.domain.SystemDictionaryDetail;
import org.mzy.org.domain.SystemDictionaryType;
import org.mzy.org.query.EmployeeQuery;
import org.mzy.org.query.SystemDictionaryDetailQuery;
import org.mzy.org.service.ISystemDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Package: org.mzy.org.controller
 * @ClassName: SystemDictionaryDetailController
 * @Date:2022/10/13 9:09
 * @Description:
 */

@RestController
@RequestMapping("/systemDictionaryDetail")
@Api(value = "数据字典明细的API", description = "相关的CRUD功能")
public class SystemDictionaryDetailController {
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    // 查询全部
    @GetMapping
    @ApiOperation(value = "查询所有信息")
    @PreAuthorize(name = "查询所有数据字典明细信息", sn = "systemDictionaryDetail:findAll")
    public List<SystemDictionaryDetail> findAll() {
        return systemDictionaryDetailService.findAll();
    }

    // 根据ID删除单条
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除一条数据")
    @PreAuthorize(name = "删除字典明细信息", sn = "systemDictionaryDetail:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            systemDictionaryDetailService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    // 批量删除
    @PatchMapping
    @ApiOperation("批量删除")
    @PreAuthorize(name = "批量删除字典明细信息", sn = "systemDictionaryDetail:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        try {
            systemDictionaryDetailService.batchDel(ids);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    // 新增或修改
    @PutMapping
    @ApiOperation(value = "新增或修改")
    @PreAuthorize(name = "新增/修改字典明细信息", sn = "systemDictionaryDetail:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail) {
        if (systemDictionaryDetail.getId() == null) {
            systemDictionaryDetailService.add(systemDictionaryDetail);
        } else {
            systemDictionaryDetailService.update(systemDictionaryDetail);
        }
        return AjaxResult.me().success();
    }

    // 根据ID查询单条
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询一条数据")
    @PreAuthorize(name = "根据id查询数据字典明细", sn = "systemDictionaryDetail:findById")
    public SystemDictionaryDetail findById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id) {
        return systemDictionaryDetailService.findById(id);
    }

    // 分页查询
    @PostMapping
    @ApiOperation("分页查询")
    @PreAuthorize(name = "分页查询数据字典明细", sn = "systemDictionaryDetail:queryByPage")
    public PageList<SystemDictionaryDetail> queryByPage(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery) {
        return systemDictionaryDetailService.queryByPage(systemDictionaryDetailQuery);
    }

    // 根据名字删除单条
    @DeleteMapping("/{name}")
    @ApiOperation(value = "根据id删除一条数据")
    @PreAuthorize(name = "删除数据字典明细", sn = "systemDictionaryDetail:delete")
    public AjaxResult delete(@PathVariable("name") String name) {
        try {
            systemDictionaryDetailService.deleteByName(name);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
}
